<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="APP类型" prop="versionType" label-width="80px">
                <el-select v-model="queryParams.versionType" placeholder="请选择APP类型" clearable size="small">
                    <el-option label="全部" value=""/>
                    <!--          <el-option label="Android" value="1" />-->
                    <!--          <el-option label="iOS" value="2" />-->
                    <el-option
                            v-for="dict in versionTypeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="版本号" prop="versionNum">
                <el-input
                        v-model="queryParams.versionNum"
                        placeholder="请输入版本号"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="版本名" prop="versionName">
                <el-input
                        v-model="queryParams.versionName"
                        placeholder="请输入版本名"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['app:version:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['app:version:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['app:version:remove']"
                >删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['app:version:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table ref="tables"  v-loading="loading" :data="versionList" @selection-change="handleSelectionChange"  :default-sort="defaultSort" @sort-change="handleSortChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序号" type="index" width="50" align="center">
                <template slot-scope="scope">
                    <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="文件名" align="center">
                <template slot-scope="scope">
                    <el-link v-if="scope.row.versionType == '1'" type="primary" :href="baseUrl + scope.row.filesUrl" :underline="false">油惠牛Android {{ scope.row.versionName }}</el-link>
                    <el-link v-if="scope.row.versionType == '2'" type="primary" :href="scope.row.filesUrl" :underline="false">油惠牛iOS {{ scope.row.versionName }}</el-link>
                </template>
            </el-table-column>
            <el-table-column label="APP类型" align="center" prop="versionType" :formatter="versionTypeFormat"/>
            <el-table-column label="版本号" sortable="custom" align="center" prop="versionNum"/>
            <el-table-column label="版本名" align="center" prop="versionName"/>
            <el-table-column label="是否强制更新" align="center" prop="forceUpdate" :formatter="forceUpdateFormat"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['app:version:edit']"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['app:version:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改APP版本更新对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="APP类型">
                    <el-radio-group v-model="form.versionType">
                        <el-radio label="1">Android</el-radio>
                        <el-radio label="2">iOS</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="版本号" prop="versionNum">
                    <el-input v-model="form.versionNum" placeholder="请输入版本号"/>
                </el-form-item>
                <el-form-item label="版本名" prop="versionName">
                    <el-input v-model="form.versionName" placeholder="请输入版本名"/>
                </el-form-item>
                <el-form-item label="是否强制更新">
                    <el-radio-group v-model="form.forceUpdate">
                        <el-radio label="0">否</el-radio>
                        <el-radio label="1">是</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="更新内容" prop="versionContent">
                    <el-input type="textarea" v-model="form.versionContent" maxlength="1000" rows="3" placeholder="请输入更新内容" show-word-limit/>
                </el-form-item>
                <el-form-item label="文件" v-if="form.versionType == '1'">
                    <!--          <fileUpload v-model="form.fils" />-->
                    <FileUploadOther v-model="form.filesUrlObj" uploadType="4"/>
                    <!--          <FileUploadServer v-model="form.fils" />-->
                </el-form-item>
                <el-form-item label="下载地址" v-if="form.versionType == '2'">
                    <el-input v-model="form.filesUrl" placeholder="请输入下载地址"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listVersion, getVersion, delVersion, addVersion, updateVersion, exportVersion } from '@/api/app/version'
import FileUpload from '@/components/FileUpload'
import FileUploadServer from '@/components/FileUploadServer'
import FileUploadOther from '@/components/FileUploadOther'

export default {
    name: 'Version',
    components: {
        FileUpload,
        FileUploadServer,
        FileUploadOther
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // baseUrl: process.env.VUE_APP_BASE_API,
            baseUrl: 'https://youhuiniu.oss-cn-beijing.aliyuncs.com/',
            // 总条数
            total: 0,
            // APP版本更新表格数据
            versionList: [],
            // APP类型数据
            versionTypeOptions: [{ dictValue: 1, dictLabel: 'Android' }, { dictValue: 2, dictLabel: 'iOS' }],
            forceUpdateOptions: [{ dictValue: 0, dictLabel: '否' }, { dictValue: 1, dictLabel: '是' }],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderByColumn:"versionNum",
                isAsc:"descending",
                versionType: null,
                versionNum: null,
                versionName: null,
                forceUpdate: null
            },
            // 默认排序
            defaultSort: {prop: 'versionNum', order: 'descending'},
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                versionType: [
                    { required: true, message: 'APP类型不能为空', trigger: 'blur' }
                ],
                versionNum: [
                    { required: true, message: '版本号不能为空', trigger: 'blur' }
                ],
                versionName: [
                    { required: true, message: '版本名不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询APP版本更新列表 */
        getList(data) {
            this.loading = true
            listVersion(this.queryParams).then(response => {
                this.versionList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        /** 排序触发事件 */
        handleSortChange(column, prop, order) {
            this.queryParams.orderByColumn = column.prop
            this.queryParams.isAsc = column.order
            this.getList()
        },
        // APP类型编码字典翻译
        versionTypeFormat(row, column) {
            return this.selectDictLabel(this.versionTypeOptions, row.versionType)
        },
        // 是否强制更新编码字典翻译
        forceUpdateFormat(row, column) {
            return this.selectDictLabel(this.forceUpdateOptions, row.forceUpdate)
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                files: null,
                filesUrl: null,
                filesUrlObj: null,
                filesName: null,
                versionType: '1',
                versionNum: null,
                versionName: null,
                forceUpdate: '0',
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                versionContent: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加APP版本更新'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getVersion(id).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改APP版本更新'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.versionType == '1' && this.form.filesUrlObj) {
                        this.form.filesUrl = this.form.filesUrlObj.objectID
                        this.form.filesName = this.form.filesUrlObj.originalName
                    }
                    // if (this.form.versionType == '1' && this.form.fils) {
                    //   this.form.filesUrl = this.form.fils.data.objectID;
                    //   this.form.filesName = this.form.fils.data.originalName;
                    // }
                    if (this.form.id != null) {
                        updateVersion(this.form).then(response => {
                            this.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addVersion(this.form).then(response => {
                            this.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$confirm('是否确认删除APP版本更新编号为"' + ids + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return delVersion(ids)
            }).then(() => {
                this.getList()
                this.msgSuccess('删除成功')
            })
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams
            this.$confirm('是否确认导出所有APP版本更新数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return exportVersion(queryParams)
            }).then(response => {
                this.download(response.msg)
            })
        }
    }
}
</script>
